<?php

namespace app\common\model\Business;

use app\common\model\Product\Product;
use app\common\model\Subject\Subject;
use think\Model;

class Collection extends Model
{
        // 表名
        protected $name = 'business_collection';
        // 忽略数据表不存在的字段
        protected $field = true;
        // 设置返回的结果类型
        protected $resultSetType = 'collection';
        // 自动写入时间戳字段
        protected $autoWriteTimestamp = 'int';
        // 定义时间戳字段名
        protected $createTime = 'createtime';
        //禁止写入时间字段
        protected $updateTime = false;
        // 软删除的字段
        protected $deleteTime = false;
        // 追加属性
        protected $append = [
                'goods',
        ];
        public function getGoodsAttr($value,$data){
                if($data['status']=='subject'){
                        $subjects = Subject::where('id',$data['collectid'])->select();
                        $subject = $subjects?$subjects->toArray()[0]:null;
                        return $subject;
                }else if($data['status']=='product'){
                        return $this->hasOne(Product::class,'id','collectid')->find();
                }
        }
        
}
